package com.example.lianxi222.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.example.lianxi222.R;
import com.example.lianxi222.adapter.CommentAdapter;
import com.example.lianxi222.adapter.MultiAdapter;
import com.example.lianxi222.databinding.FragmentVideoBinding;
import com.example.lianxi222.fragment.live.LiveActivity;
import com.example.lianxi222.fragment.video.VideoModelView;
import com.example.lianxi222.fragment.video.VideoRecAdapter;
import com.example.mvvm_base.userbean.CommentEntity;
import com.example.mvvm_base.userbean.FollowEntity;
import com.example.mvvm_base.userbean.VideoEntity;
import com.example.mvvm_base.view.BaseFragment;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.util.ArrayList;
import java.util.List;


public class VideoFragment extends BaseFragment<FragmentVideoBinding, VideoModelView> {
private List<VideoEntity.DataBean> list=new ArrayList<>();
private MultiAdapter videoRecAdapter;
private int currentPage=1;
private StandardGSYVideoPlayer player;
private int currentPosition=0;
//评论数据源
private List<CommentEntity.DataBean> commentList=new ArrayList<>();
//评论适配器
private CommentAdapter commentAdapter;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_video;
    }

    @Override
    protected void initData() {
        //视频
     viewModel.getVideo(currentPage,10);
     //视频成功
     viewModel.videoSuccess.observe(this, videoEntity -> {
         if(videoEntity.getCode()==200){
             list.clear();
             list.addAll(videoEntity.getData());
             videoRecAdapter.getData().addAll(videoEntity.getData());
             videoRecAdapter.notifyItemChanged(currentPosition);
         }else{
             ToastUtils.showLong(videoEntity.getMessage());
         }
     });
     //视频失败
     viewModel.videoFailure.observe(this, s -> ToastUtils.showLong(s));
     //评论成功
     viewModel.commentSuccess.observe(this, commentEntity -> {
         if(commentEntity.getCode()==200){
             Log.d("sss", "onChanged: "+commentEntity.getMessage());
             commentList.clear();
             commentList.addAll(commentEntity.getData());
             commentAdapter.notifyItemChanged(currentPosition);

         }
     });
     //评论失败
     viewModel.videoFailure.observe(this, s -> ToastUtils.showLong(s));
     //点赞成功
        viewModel.likeSuccess.observe(this, likeEntity -> {
            if(likeEntity.getCode()==200){
                ToastUtils.showLong(likeEntity.getMessage());
                list.get(currentPosition).setDianzan(1);
                videoRecAdapter.notifyItemChanged(currentPosition);
            }else{
                ToastUtils.showLong(likeEntity.getMessage());
            }
        });
        viewModel.likeFailure.observe(this, s -> ToastUtils.showLong(s));
        //不喜欢
        viewModel.unLikeSuccess.observe(this, likeEntity -> {
            if(likeEntity.getCode()==200){
                ToastUtils.showLong(likeEntity.getMessage());
                list.get(currentPosition).setDianzan(0);
                videoRecAdapter.notifyItemChanged(currentPosition);
            }else{
                ToastUtils.showLong(likeEntity.getMessage());
            }
        });
        viewModel.unLikeFailure.observe(this, s -> ToastUtils.showLong(s));
        //关注
        viewModel.followS.observe(this, followEntity -> {
            if(followEntity.getCode()==200){
                list.get(currentPosition).setGuanzhu(1);
                videoRecAdapter.notifyItemChanged(currentPosition);
                ToastUtils.showLong(followEntity.getMessage());
            }else{
                ToastUtils.showLong(followEntity.getMessage());
            }
        });
        viewModel.followF.observe(this, s -> ToastUtils.showLong(s));
        //发送评论
        viewModel.sendSuccess.observe(this, commentEntity -> {
       if(commentEntity.getCode()==200){
           //将发送成功的评论获取添加到集合
           commentAdapter.getData().addAll(0,commentEntity.getData());
           //刷新适配器
               commentAdapter.notifyDataSetChanged();
          //获取评论的列表控件
               RecyclerView recyclerView= (RecyclerView) videoRecAdapter.getViewByPosition(currentPosition,R.id.comment_rec);
          //使每次获取完刷新后滚动到第一条评论的位置
               if(recyclerView!=null){
              recyclerView.scrollToPosition(0);
          }
               //将输入框清空
           EditText et1= (EditText) videoRecAdapter.getViewByPosition(currentPosition,R.id.comment_edit);
              assert(et1!=null);
               et1.setText("");

       }else{
           ToastUtils.showLong(commentEntity.getMessage());
       }
        });
        viewModel.sendFailure.observe(this, s -> ToastUtils.showLong(s));
    }

    @Override
    protected void initView() {
        PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
        pagerSnapHelper.attachToRecyclerView(dataBinding.videoRec);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(requireContext());
        dataBinding.videoRec.setLayoutManager(linearLayoutManager);
        videoRecAdapter=new MultiAdapter();
        videoRecAdapter.getData().addAll(list);
    dataBinding.videoRec.setAdapter(videoRecAdapter);
     dataBinding.videoRec.addOnScrollListener(new RecyclerView.OnScrollListener() {
         @Override
         public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
             super.onScrolled(recyclerView, dx, dy);
             currentPosition=linearLayoutManager.findFirstVisibleItemPosition();
             player= (StandardGSYVideoPlayer) videoRecAdapter.getViewByPosition(currentPosition,R.id.video_player);
             if(player!=null){
                 player.startPlayLogic();
             }
         }
     });
     videoRecAdapter.addChildClickViewIds(R.id.item_video_live_iv,R.id.comment_icon,R.id.close_view,R.id.like_icon,R.id.video_add,R.id.comment_send,R.id.video_share_icon);
     videoRecAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
         @Override
         public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
             switch (view.getId()){
                 case R.id.comment_icon:
                     //设置成可见
                     View commonLayout = videoRecAdapter.getViewByPosition(currentPosition, R.id.comment_view);
                     commonLayout.setVisibility(View.VISIBLE);
                     Animation animationIn = AnimationUtils.loadAnimation(requireContext(), R.anim.animin);
                     animationIn.setDuration(500);
                     animationIn.setInterpolator(new AccelerateInterpolator());
                     animationIn.setAnimationListener(new Animation.AnimationListener() {
                         @Override
                         public void onAnimationStart(Animation animation) {
                             VideoEntity.DataBean dataBean = list.get(currentPosition);
                             viewModel.getComment(dataBean.getId());
                            dataBinding.videoRec.setIntercept(false);
                         }

                         @Override
                         public void onAnimationEnd(Animation animation) {

                         }

                         @Override
                         public void onAnimationRepeat(Animation animation) {

                         }
                     });
                     commonLayout.startAnimation(animationIn);
                     commentAdapter=new CommentAdapter(commentList);
                    RecyclerView recyclerView= (RecyclerView) videoRecAdapter.getViewByPosition(currentPosition,R.id.comment_rec);
                    recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
                    recyclerView.setAdapter(commentAdapter);
                     break;
                 case R.id.close_view:
                     View commonLayout2 = videoRecAdapter.getViewByPosition(position, R.id.comment_view);
                     commonLayout2.setVisibility(View.VISIBLE);
                     Animation animationOut = AnimationUtils.loadAnimation(requireContext(), R.anim.animout);
                     animationOut.setDuration(500);
                     animationOut.setInterpolator(new AccelerateInterpolator());
                     animationOut.setAnimationListener(new Animation.AnimationListener() {
                         @Override
                         public void onAnimationStart(Animation animation) {
                             dataBinding.videoRec.setIntercept(true);
                         }

                         @Override
                         public void onAnimationEnd(Animation animation) {
                               commonLayout2.setVisibility(View.GONE);
                         }

                         @Override
                         public void onAnimationRepeat(Animation animation) {

                         }
                     });
                     commonLayout2.startAnimation(animationOut);
                     break;
                 case R.id.like_icon:
                     VideoEntity.DataBean dataBean = list.get(currentPosition);
                     if(dataBean.getDianzan()==0){
                         viewModel.like(dataBean.getId());
                     }else{
                         viewModel.unLike(dataBean.getId());
                     }
                     break;
                 case R.id.video_add:
                     VideoEntity.DataBean dataBean1 = list.get(currentPosition);
                     viewModel.follow(dataBean1.getAuthname());
                     break;
                 case R.id.comment_send:
                     VideoEntity.DataBean dataBean2 = list.get(currentPosition);
                    EditText sendMsg = (EditText) videoRecAdapter.getViewByPosition(currentPosition,R.id.comment_edit);
                    if(sendMsg!=null){
                        String s = sendMsg.getText().toString();
                        if(TextUtils.isEmpty(s)){
                            ToastUtils.showLong("请输入评论，注意您的用语");
                            return;
                        }else{
                            viewModel.send(s,dataBean2.getId());
                        }
                    }
                    break;
                 case R.id.video_share_icon:
                     VideoEntity.DataBean dataBean3 = list.get(currentPosition);
                     UMImage umImage=new UMImage(requireContext(),dataBean3.getVideomainimg());
                     umImage.compressStyle=UMImage.CompressStyle.SCALE;
                     UMWeb web =new UMWeb(dataBean3.getVideopath());
                     web.setTitle(dataBean3.getAuthname());//标题
                     web.setThumb(umImage);//缩略图
                     web.setDescription(dataBean3.getCaption());//描述
                     new ShareAction(requireActivity())
                             .withMedia(web)
                             .open();
                     break;
                 case R.id.item_video_live_iv:
                     VideoEntity.DataBean dataBean4 = list.get(currentPosition);
                     Intent intent = new Intent(getContext(), LiveActivity.class);
                     intent.putExtra("videoBean",dataBean4);
                     startActivity(intent);
                     break;

             }
         }
     });

    }

    @Override
    protected Class<VideoModelView> getViewModel() {
        return VideoModelView.class;
    }
}